﻿#include "dlgstart.h"
#include "ui_dlgstart.h"

#include <QDesktopWidget>

quint16 g_nscreenwidth = 0;
quint16 g_nscreenheight = 0;
quint16 g_backImgWidth = 0;
quint16 g_backImgHeight = 0;
quint16 g_backImgTop = 0;
quint16 g_backImgLeft = 0;

DlgStart::DlgStart(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgStart)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    QDesktopWidget* desktopWidget = QApplication::desktop();
    QRect applicationRect = desktopWidget->screenGeometry();
    g_nscreenwidth = applicationRect.width();
    g_nscreenheight = applicationRect.height();
    //
    m_bottonScreen = NULL;
    m_quick = new QQuickWidget(this);
    m_quick->setResizeMode(QQuickWidget::SizeRootObjectToView);
    m_quick->setAttribute(Qt::WA_AlwaysStackOnTop);
    m_quick->setClearColor(QColor(Qt::transparent));
    /////
    if(desktopWidget->screenCount() > 1){
        m_bHaveExScreen = true;
        QRect rcShow = desktopWidget->screenGeometry(1);
        this->setGeometry(rcShow);
        g_nscreenwidth = rcShow.width();
        g_nscreenheight = rcShow.height();
        m_quick->setFixedSize(g_nscreenwidth,g_nscreenheight);
        this->show();
    }else{
        m_bHaveExScreen = false;
        this->setFixedSize(g_nscreenwidth,g_nscreenheight);
        m_quick->setFixedSize(g_nscreenwidth,g_nscreenheight);
        this->show();
//        QObject::connect(&m_timerScreen, &QTimer::timeout, [=] () {

//        });
//        m_timerScreen.setSingleShot(true);
//        m_timerScreen.start(1500);
    }

    qDebug() << u8"wall 屏幕分辨率 width=" << g_nscreenwidth << "   height=" << g_nscreenheight << u8"屏幕个数=" << desktopWidget->screenCount();

    QDir dir;
    m_masterDir = dir.currentPath();
    loadLayout();
    Init();
    loadUI();

    m_netcomm = new netComm(this);
    m_bRobotStartSuc = false;
    m_nLoadingCount = 0;
    m_objNginx = NULL;
    m_nttsType = 0;
    if(!m_timer.isActive())
        m_timer.start(2000);
    //
    m_bStartNginx = false;
    //
    ////    QUrl source("qrc:/maximumemitted.qml");
//    QUrl source("./wallqml/maximumemitted.qml");
//    m_quick->setSource(source);
//    m_quick->show();

    m_quick->hide();
}

DlgStart::~DlgStart()
{
    UnInit();
    delete ui;
}

void DlgStart::Init()
{
    m_websocketServer = new websocketserver(33662,this);
    connect(m_websocketServer,&websocketserver::sigWallRun,this,&DlgStart::slotWallRun,Qt::QueuedConnection);
    connect(&m_timer,&QTimer::timeout,this,&DlgStart::slotTimerout,Qt::QueuedConnection);
    connect(&m_signalTimer,&QTimer::timeout,this,&DlgStart::slotLastTimer,Qt::QueuedConnection);
    connect(&m_ttsStateTimer,&QTimer::timeout,this,&DlgStart::slottsStateTimer,Qt::QueuedConnection);
    m_bOnebyOnewall = true;
    m_bStartRite = false;
    //create db
    m_db = QSqlDatabase::addDatabase("QSQLITE");
    m_db.setDatabaseName("wall.db");
    if(m_db.open())
    {
        m_dbquery = new QSqlQuery(m_db);
        m_szLog = QString("CREATE TABLE `t_wallLabel` ("
                          "`userId`	INTEGER PRIMARY KEY AUTOINCREMENT,"
                          "`left`	INTEGER,"
                          "`top`	INTEGER,"
                          "`width`	INTEGER,"
                          "`height`	INTEGER,"
                          "`imgwidth`	INTEGER,"
                          "`imgheight`	INTEGER,"
                          "`signdname`	INTEGER);");
        m_dbquery->exec(m_szLog);
        m_szLog = QString("CREATE TABLE `t_basiconfig` ("
                          "`userid`	INTEGER PRIMARY KEY,"
                          "`bStartRite`	BLOB,"
                          "`onebyoneupway` BLOB,"
                          "`activityName`	TEXT,"
                          "`activityNum`	TEXT,"
                          "`activityPwd`	TEXT,"
                          "`signInBackMusic`	TEXT,"
                          "`signSucBackMusic`	TEXT,"
                          "`brushColor`	TEXT,"
                          "`speakername`	TEXT,"
                          "`speakerSpeed`	TEXT,"
                          "`speakerPicth`	TEXT,"
                          "`beginTts`	TEXT,"
                          "`randomTts1`	TEXT,"
                          "`randomTts2`	TEXT,"
                          "`randomTts3`	TEXT,"
                          "`backimgwidth` TEXT,"
                          "`backimgheight` TEXT,"
                          "`endTts`	TEXT);");
        m_dbquery->exec(m_szLog);
    }
    m_szLog = QString("select * from t_wallLabel");
    if(m_dbquery->exec(m_szLog))
    {
        int nleft = m_dbquery->record().indexOf("left");
        int ntop = m_dbquery->record().indexOf("top");
        int nwidth = m_dbquery->record().indexOf("width");
        int nheight = m_dbquery->record().indexOf("height");
        int nimgwidth = m_dbquery->record().indexOf("imgwidth");
        int nimgheight = m_dbquery->record().indexOf("imgheight");
        int nsigndname = m_dbquery->record().indexOf("signdname");
        while(m_dbquery->next())
        {
            QString signdname = m_dbquery->value(nsigndname).toString();
            int left = m_dbquery->value(nleft).toInt();
            int top = m_dbquery->value(ntop).toInt();
            int width = m_dbquery->value(nwidth).toInt();
            int height = m_dbquery->value(nheight).toInt();
            int imgwidth = m_dbquery->value(nimgwidth).toInt();
            int imgheight = m_dbquery->value(nimgheight).toInt();
            //
            QLabel *label = new QLabel(this);
//            label->setObjectName("label");
//            label->setScaledContents(true);
//            label->setStyleSheet("border:2px solid red;");
            //
            int nWidthRatio = g_backImgWidth / imgwidth;
            if((imgwidth - g_backImgWidth) < 5 && (imgwidth - g_backImgWidth) > 0)
                nWidthRatio = 1;
            int nHeightRatio = g_backImgHeight / imgheight;
            left = left*nWidthRatio + g_backImgLeft;
            top = top*nHeightRatio + g_backImgTop;
            width = width*nWidthRatio;
            height = height*nHeightRatio;
            //
            label->setGeometry(left,top,width,height);
            qDebug() << u8"初始化时的矩形框 坐标x=" << nleft << ntop << nwidth << nheight << ":"
                     << nWidthRatio << g_backImgWidth << imgwidth << ":"
                     << nHeightRatio << g_backImgHeight << imgheight;
//            label->setText(signdname);
            wallinfo* info = new wallinfo();
            info->label = label;
            info->name = signdname;
            info->bIsUpWallReady = false;
            info->bIsUpWallOver = false;
            m_vector.push_back(info);
            label->raise();
            label->show();
        }
    }
    m_szLog = QString("select * from t_basiconfig");
    if(m_dbquery->exec(m_szLog))
    {
        int nonebyoneupway = m_dbquery->record().indexOf("onebyoneupway");
        while(m_dbquery->next())
        {
            m_bOnebyOnewall = m_dbquery->value(nonebyoneupway).toBool();
        }
    }
}

void DlgStart::loadLayout()
{
    m_label_back = new QLabel(this);
    m_label_back->setFixedSize(g_nscreenwidth,g_nscreenheight);
    m_label_back->setAlignment(Qt::AlignCenter);
    m_label_back->setObjectName("m_label_back");
    m_label_back->setStyleSheet("#m_label_back{background:black;}");
    m_label_back->lower();
    m_layoutglobal = new QVBoxLayout(this);
    m_layoutglobal->addWidget(m_label_back);
    m_layoutglobal->setSpacing(0);
    m_layoutglobal->setMargin(0);
    //
    QImage imgback("./httpserver/html/Image/background.png");
    QImage tmpimg = imgback.scaled(m_label_back->size(),Qt::KeepAspectRatio);
    g_backImgWidth = tmpimg.width();
    g_backImgHeight = tmpimg.height();
    m_label_back->setPixmap(QPixmap::fromImage(tmpimg));
    g_backImgTop = (g_nscreenheight - tmpimg.height())/2;
    g_backImgLeft = (g_nscreenwidth - tmpimg.width())/2;
    qDebug() << u8"g_backImgLeft1=" << g_backImgLeft;
    if(m_bHaveExScreen){
        m_bottonScreen = new DlgBottomScreen();
        m_bottonScreen->showFullScreen();
        m_bottonScreen->setStyleSheet("#DlgBottomScreen { border-image: url(\"./httpserver/html/Image/background.png\"); }");
    }
}

void DlgStart::loadUI()
{
    //    QString filename = QString("MainWindow.css");
    //    QFile fileSkin(filename);
    //    if(fileSkin.open(QIODevice::ReadOnly))
    //    {
    //        QString strSkin = QString::fromUtf8(fileSkin.readAll());
    //        this->setStyleSheet(strSkin);
    //        fileSkin.close();
    //    }
}

void DlgStart::UnInit()
{

}

void DlgStart::clearVector()
{
    while(!m_vector.isEmpty()){
        wallinfo* p = m_vector.takeFirst();
        if(p->label) {
            delete p->label;
            p->label = NULL;
        }
        delete p;
        p = NULL;
    }
    m_szLog = QString("delete from t_wallLabel");
    m_dbquery->exec(m_szLog);
}

void DlgStart::slotWallRun(int nType, QString data)
{
    if(0 == nType){
        //recv background change
        QImage imgback("./httpserver/html/Image/background.png");
        QImage tmpimg = imgback.scaled(m_label_back->size(),Qt::KeepAspectRatio);
        g_backImgWidth = tmpimg.width();
        g_backImgHeight = tmpimg.height();
        m_label_back->setPixmap(QPixmap::fromImage(tmpimg));
        g_backImgTop = (g_nscreenheight - tmpimg.height())/2;
        g_backImgLeft = (g_nscreenwidth - tmpimg.width())/2;
        //
        qDebug() << u8"g_backImgLeft2=" << g_backImgLeft;
        this->update();
    }else if(1 == nType){
        //coordsLabs recv
        clearVector();
        QJsonParseError error;
        QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8(), &error);
        if(QJsonParseError::NoError != error.error)
            return;
        if(!doc.isObject())
            return;
        QJsonObject obj = doc.object();
        QJsonArray array = obj.take("data").toArray();
        bool bOnebyoneupway = obj.take("onebyoneupway").toBool();
        m_bOnebyOnewall = bOnebyoneupway;
        QString activityName = obj.take("activityName").toString();
        QString activityNum = obj.take("activityNum").toString();
        QString activityPwd = obj.take("activityPwd").toString();
        QString signInBackMusic = obj.take("signInBackMusic").toString();
        QString signSucBackMusic = obj.take("signSucBackMusic").toString();
        QString brushColor = obj.take("brushColor").toString();
        QString speakername = obj.take("speakername").toString();
        QString speakerSpeed = obj.take("speakerSpeed").toString();
        QString speakerPicth = obj.take("speakerPicth").toString();
        QString beginTts = obj.take("beginTts").toString();
        QString randomTts1 = obj.take("randomTts1").toString();
        QString randomTts2 = obj.take("randomTts2").toString();
        QString randomTts3 = obj.take("randomTts3").toString();
        QString endTts = obj.take("endTts").toString();
        QString backimgwidth = obj.take("backimgwidth").toString();
        QString backimgheight = obj.take("backimgheight").toString();
        m_szLog = QString("delete from t_basiconfig");
        m_dbquery->exec(m_szLog);
        m_szLog = QString("insert into t_basiconfig(userid,activityName,activityNum,activityPwd,signInBackMusic,signSucBackMusic,brushColor,speakername,speakerSpeed,speakerPicth,beginTts,randomTts1,randomTts2,randomTts3,endTts,backimgwidth,backimgheight,onebyoneupway)"
                          "values (1,'%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13','%14','%15','%16','%17',%18)")
                .arg(activityName).arg(activityNum).arg(activityPwd).arg(signInBackMusic).arg(signSucBackMusic).arg(brushColor)
                .arg(speakername).arg(speakerSpeed).arg(speakerPicth).arg(beginTts).arg(randomTts1).arg(randomTts2).arg(randomTts3).arg(endTts).arg(backimgwidth).arg(backimgheight).arg(bOnebyoneupway);
        m_dbquery->exec(m_szLog);
        for(int i=0; i<array.size(); i++){
            QJsonObject lobj = array.at(i).toObject();
            int nleft = lobj.take("left").toInt();
            int ntop = lobj.take("top").toInt();
            int nwidth = lobj.take("width").toInt();
            int nheight = lobj.take("height").toInt();
            int nimgwidth = lobj.take("imgwidth").toInt();
            int nimgheight = lobj.take("imgheight").toInt();
            QString strname = lobj.take("name").toString();
            //
            m_szLog = QString("insert into t_wallLabel(left,top,width,height,signdname,imgwidth,imgheight)"
                                    "values (%1,%2,%3,%4,'%5',%6,%7)")
                    .arg(nleft).arg(ntop).arg(nwidth).arg(nheight).arg(strname).arg(nimgwidth).arg(nimgheight);
            m_dbquery->exec(m_szLog);
            //
            QLabel *label = new QLabel(this);
            int nWidthRatio = g_backImgWidth / nimgwidth;
            if((nimgwidth - g_backImgWidth) < 5 && (nimgwidth - g_backImgWidth) > 0)
                nWidthRatio = 1;
            int nHeightRatio = g_backImgHeight / nimgheight;
            nleft = nleft*nWidthRatio + g_backImgLeft;
            ntop = ntop*nHeightRatio + g_backImgTop;
            nwidth = nwidth*nWidthRatio;
            nheight = nheight*nHeightRatio;
            label->setGeometry(nleft,ntop,nwidth,nheight);

            qDebug() << u8"接收到上传的矩形框 坐标x=" << nleft << ntop << nwidth << nheight;

            wallinfo* info = new wallinfo();
            info->label = label;
            info->name = strname;
            info->bIsUpWallReady = false;
            info->bIsUpWallOver = false;
            m_vector.push_back(info);
            label->show();
        }
        this->update();
    }else if(2 == nType){
        //catapult to wall
        if(!m_vector.isEmpty()) {
            QJsonParseError error;
            QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8(), &error);
            if(QJsonParseError::NoError != error.error)
                return;
            if(!doc.isObject())
                return;
            QJsonObject obj = doc.object();
            QString szPicName = obj.take("picpath").toString();
            QString name = obj.take("name").toString();
            for(int i=0; i<m_vector.size(); i++){
                wallinfo* info = m_vector.at(i);
                if(info){
                    if(!info->bIsUpWallReady){
                        if((u8"随机位置" == name && info->name.length() < 1)
                                || info->name == name){
                            info->bIsUpWallReady = true;
                            info->label->setAlignment(Qt::AlignCenter);
                            QImage imgwrite(szPicName);
                            QImage tmpimg = imgwrite.scaled(info->label->size(),Qt::KeepAspectRatio);
                            info->label->setPixmap(QPixmap::fromImage(tmpimg));
                            info->label->hide();
                            qDebug() << u8"接收到一个平板端消息name=" << name << "label name=" << info->name << "x=" << info->label->x() << "; y=" << info->label->y();
                            break;
                        }
                    }
                }
            }
        }
    }else if(3 == nType){
        //ctrlactivity bStart
        QJsonParseError error;
        QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8(), &error);
        if(QJsonParseError::NoError != error.error)
            return;
        if(!doc.isObject())
            return;
        QJsonObject obj = doc.object();
        bool bStart = obj.take("bStart").toBool();
        m_bStartRite = bStart;
        ctrlActivity(bStart);
    }else if(4 == nType){
        //ctrl quit
        QJsonParseError error;
        QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8(), &error);
        if(QJsonParseError::NoError != error.error)
            return;
        if(!doc.isObject())
            return;
        QJsonObject obj = doc.object();
        bool bQuit = obj.take("bQuit").toBool();
        if(NULL != m_bottonScreen){
            if(bQuit){
                m_bottonScreen->hide();
            }else{
                m_bottonScreen->showFullScreen();
            }
        }
    }
}

void DlgStart::slotTimerout()
{
    m_nLoadingCount++;
    if(500 < m_nLoadingCount)
        m_nLoadingCount = 20;
    if(!m_bRobotStartSuc){
        if(1 == m_nLoadingCount){
            m_netcomm->InitRobotCoreComm();
            m_netcomm->ConnectRobotCoreComm();
        }else if(2 == m_nLoadingCount){
            m_netcomm->InitEngine();
        }
    }
    if(!m_bStartNginx){
        if(1 == m_nLoadingCount){
            QDir dir;
            QString strNginxPath = dir.currentPath() + "/httpserver";
            QString strNginxStopCmd = "nginx_wall.exe -s stop";
            if(NULL == m_objNginx)
                m_objNginx = new QProcess;
            QDir::setCurrent(strNginxPath);
            m_objNginx->setWorkingDirectory(strNginxPath);
            m_objNginx->startDetached(strNginxStopCmd);
            m_objNginx->waitForFinished();
            QDir::setCurrent(m_masterDir);
        }
        if(10 == m_nLoadingCount){
            m_bStartNginx = true;

            QDir dir;
            QString strNginxPath = dir.currentPath() + "/httpserver";
            QString strNginxStartCmd = "cmd /c start nginx_wall.exe";
            if(NULL == m_objNginx)
                m_objNginx = new QProcess;
            QDir::setCurrent(strNginxPath);
            m_objNginx->setWorkingDirectory(strNginxPath);
            m_objNginx->startDetached(strNginxStartCmd);
            m_objNginx->waitForStarted();
            QDir::setCurrent(m_masterDir);
        }
        return;
    }
    if(m_bOnebyOnewall){
        //启动一个一个上墙
        for(int i=0; i<m_vector.size(); i++){
            wallinfo* p = m_vector.at(i);
            if(p){
                if(!p->bIsUpWallOver && p->bIsUpWallReady){
                    p->bIsUpWallOver = true;
                    qDebug() << u8"逐一上墙,最后一个签约完毕,播放结束语及成功音乐" << p->label->x() << ":" << p->label->y();
                    sequentialAnimation(p->label->x(),p->label->y(),p->label);
                    if(i == m_vector.size() - 1){
                        m_signalTimer.setSingleShot(true);
                        m_signalTimer.start(2000);
                    }
                    break;
                }
            }
        }
    }else{
        //启动都签完再上墙
        int nCount = 0;
        for(int i=0; i<m_vector.size(); i++){
            wallinfo* p = m_vector.at(i);
            if(p){
                if(p->bIsUpWallReady){
                    nCount++;
                }
            }
        }
        if(nCount !=0 && (nCount == m_vector.size())){
            //都签约完毕，启动一起飞出画面.
            parallelAnimation();
            m_timer.stop();
        }
    }
}

void DlgStart::slotLastTimer()
{
    //逐一上墙,最后一个签约完毕,播放结束语及成功音乐
    m_netcomm->SendBehaviour(m_endTts, speakername, speakerSpeed, speakerPicth, m_sucmusicPath);
    m_timer.stop();
    QUrl source("./wallqml/maximumemitted.qml");
    m_quick->setSource(source);
    m_quick->show();
}

void DlgStart::sequentialAnimation(int originX, int originY, QLabel *label)
{
    int firstDuration = 1500;
    int secondDuration = 500;
    QRect r;
    switch (qrand()%7) {
    case 0: //top left
        r.setTopLeft(QPoint(-label->width(),-label->height()));
        break;
    case 1: //top center
        r.setTopLeft(QPoint((m_label_back->width()-label->width())/2,-label->height()));
        break;
    case 2: //top right
        r.setTopLeft(QPoint(m_label_back->width()+label->width(),-label->height()));
        break;
    case 3: //left center
        r.setTopLeft(QPoint(-label->width(),(m_label_back->height()-label->height())/2));
        break;
    case 4: //bottom left
        r.setTopLeft(QPoint(-label->width(),m_label_back->height()+label->height()));
        break;
    case 5: //bottom center
        r.setTopLeft(QPoint((m_label_back->width()-label->width())/2,m_label_back->height()+label->height()));
        break;
    case 6: //bottom right
        r.setTopLeft(QPoint(m_label_back->width()+label->width(),m_label_back->height()+label->height()));
        break;
    default: //right center
        r.setTopLeft(QPoint(m_label_back->width()+label->width(),(m_label_back->height()-label->height())/2));
        break;
    }
    r.setWidth(label->width());
    r.setHeight(label->height());
    //第一部分动画：从屏幕的不同位置飞向中心点
    QPropertyAnimation *first = new QPropertyAnimation(label,"geometry");
    first->setDuration(firstDuration);
    first->setStartValue(QRect(r.x(),r.y(),0,0));
    first->setEndValue(QRect(
                       (m_label_back->width()-label->width())/2,
                       (m_label_back->height()-label->height())/2,
                       label->width(),
                       label->height()));
    first->setEasingCurve(QEasingCurve::InOutQuad);
    //第二部分动画：从中间位置飞向原本的位置
    QPropertyAnimation *second = new QPropertyAnimation(label,"geometry");
    second->setDuration(secondDuration);
    second->setEndValue(QRect(
                            originX,
                            originY,
                            label->width(),
                            label->height()));
    second->setEasingCurve(QEasingCurve::InQuint);
    //将第一、二部分动画串行组
    QSequentialAnimationGroup *group = new QSequentialAnimationGroup(label);
    group->addAnimation(first);
    group->addAnimation(second);
    connect(group,&QSequentialAnimationGroup::finished,group,&QSequentialAnimationGroup::deleteLater);
//    if(m_bOnebyOnewall){
//        connect(group,&QSequentialAnimationGroup::finished,[=](){
//        });
//    }

    group->start();
    label->show();
}

void DlgStart::parallelAnimation()
{
    QParallelAnimationGroup *group = new QParallelAnimationGroup();
    for(int i=0; i<m_vector.size(); i++){
        wallinfo* p = m_vector.at(i);
        if(p){
            QRect r;
            switch (qrand()%7) {
            case 0: //top left
                r.setTopLeft(QPoint(-p->label->width(),-p->label->height()));
                break;
            case 1: //top center
                r.setTopLeft(QPoint((m_label_back->width()-p->label->width())/2,-p->label->height()));
                break;
            case 2: //top right
                r.setTopLeft(QPoint(m_label_back->width()+p->label->width(),-p->label->height()));
                break;
            case 3: //left center
                r.setTopLeft(QPoint(-p->label->width(),(m_label_back->height()-p->label->height())/2));
                break;
            case 4: //bottom left
                r.setTopLeft(QPoint(-p->label->width(),m_label_back->height()+p->label->height()));
                break;
            case 5: //bottom center
                r.setTopLeft(QPoint((m_label_back->width()-p->label->width())/2,m_label_back->height()+p->label->height()));
                break;
            case 6: //bottom right
                r.setTopLeft(QPoint(m_label_back->width()+p->label->width(),m_label_back->height()+p->label->height()));
                break;
            default: //right center
                r.setTopLeft(QPoint(m_label_back->width()+p->label->width(),(m_label_back->height()-p->label->height())/2));
                break;
            }
            r.setWidth(p->label->width());
            r.setHeight(p->label->height());
            //动画：从屏幕的不同位置飞向原本的位置
            QPropertyAnimation *first = new QPropertyAnimation(p->label,"geometry");
            first->setDuration(2000);
            first->setStartValue(QRect(r.x(),r.y(),0,0));
            first->setEndValue(QRect(
                                   p->label->x(),
                                   p->label->y(),
                                   p->label->width(),
                                   p->label->height()));
            first->setEasingCurve(QEasingCurve::InOutQuad);
            group->addAnimation(first);
            p->label->show();
        }
    }
    connect(group,&QParallelAnimationGroup::finished,group,&QParallelAnimationGroup::deleteLater);
    connect(group,&QParallelAnimationGroup::finished, [=] () {
        //同时上墙完毕.
        m_netcomm->SendBehaviour(m_endTts, speakername, speakerSpeed, speakerPicth, m_sucmusicPath);
        QUrl source("./wallqml/maximumemitted.qml");
        m_quick->setSource(source);
        m_quick->show();
    });
    group->start();
}

void DlgStart::ctrlActivity(bool bStart)
{
    //1.停止语音、视频、tts等的播放；
    m_netcomm->StopTTS();
    QString strCurPath = QDir::currentPath();
    QString backmusic = "";
    QString sucmusic = "";
    QString beginTts = "";
    m_szLog = QString("select * from t_basiconfig");
    if(m_dbquery->exec(m_szLog))
    {
        int nsignInBackMusic = m_dbquery->record().indexOf("signInBackMusic");
        int nsignSucBackMusic = m_dbquery->record().indexOf("signSucBackMusic");
        int nspeakername = m_dbquery->record().indexOf("speakername");
        int nspeakerSpeed = m_dbquery->record().indexOf("speakerSpeed");
        int nspeakerPicth = m_dbquery->record().indexOf("speakerPicth");
        int nbeginTts = m_dbquery->record().indexOf("beginTts");
        int nendTts = m_dbquery->record().indexOf("endTts");
        int nrandomTts1 = m_dbquery->record().indexOf("randomTts1");
        int nrandomTts2 = m_dbquery->record().indexOf("randomTts2");
        int nrandomTts3 = m_dbquery->record().indexOf("randomTts3");
        while(m_dbquery->next())
        {
            backmusic = m_dbquery->value(nsignInBackMusic).toString();
            sucmusic = m_dbquery->value(nsignSucBackMusic).toString();
            speakername = m_dbquery->value(nspeakername).toString();
            speakerSpeed = m_dbquery->value(nspeakerSpeed).toInt();
            speakerPicth = m_dbquery->value(nspeakerPicth).toInt();
            beginTts = m_dbquery->value(nbeginTts).toString();
            m_endTts = m_dbquery->value(nendTts).toString();
            m_randomtts1 = m_dbquery->value(nrandomTts1).toString();
            m_randomtts2 = m_dbquery->value(nrandomTts2).toString();
            m_randomtts3 = m_dbquery->value(nrandomTts3).toString();
        }
    }
    m_backmusicPath = strCurPath+"/signwall_backfiles/backgroundmusic/"+backmusic;
    m_sucmusicPath = strCurPath+"/signwall_backfiles/successmusic/"+sucmusic;
    if(bStart){
        //控制活动开始 1.启动音乐、视频、tts等的播放；2.删除已签名的人员；3.界面恢复到初始状态；
        if(!m_vector.isEmpty()){
            for(int i=0; i<m_vector.size(); i++){
                wallinfo* p = m_vector.at(i);
                if(p->label) {
                    p->label->clear();
                    p->bIsUpWallReady = false;
                    p->bIsUpWallOver = false;
                }
            }
        }
        this->update();
        m_netcomm->SendBehaviour(beginTts, speakername, speakerSpeed, speakerPicth, "");
        m_nttsType = 1;
        if(!m_ttsStateTimer.isActive()){
            m_ttsStateTimer.start(500);
        }
        if(!m_timer.isActive())
            m_timer.start(2000);
        if(!m_netcomm->m_timerautoAction.isActive())
            m_netcomm->m_timerautoAction.start(5000);
    }else{
        //控制活动停止
        m_nttsType = 2;
        m_timer.stop();
        m_signalTimer.stop();
        m_quick->hide();
        m_netcomm->m_timerautoAction.stop();
    }
}

void DlgStart::slottsStateTimer()
{
    if(0 == m_nttsType)
        return;
    if(1 == m_nttsType){
        if(m_netcomm->IsEndTTs()){
            //开场语说完了,播放随机语
            int nrandom = qrand()%3;
            QString tts;
            if(0 == nrandom){
                tts = m_randomtts1;
            }else if(1 == nrandom){
                tts = m_randomtts2;
            }else{
                tts = m_randomtts3;
            }
            m_nttsType = 3;
            m_netcomm->SendBehaviour(tts, speakername, speakerSpeed, speakerPicth, m_backmusicPath);
            m_ttsStateTimer.stop();
        }
    }
}
